home *** CD-ROM | disk | FTP | other *** search
/ FishMarket 1.0 / FishMarket v1.0.iso / fishies / 076-100 / disk_091 / adlrun / adlmach.c < prev    next >
C/C++ Source or Header  |  1992-05-06  |  6KB  |  361 lines

  1. #include <stdio.h>
  2.  
  3. #include "adltypes.h"
  4. #include "adlprog.h"
  5. #include "adlrun.h"
  6. #include "adldef.h"
  7. #include "virtmem.h"
  8.  
  9.  
  10. extern
  11. struct    pagetab    codetab;
  12.  
  13. extern
  14. int16    filenum,    /* From adlerr.c */
  15.     linenum,    /* From adlerr.c */
  16.     foundfile;    /* From adlerr.c */
  17.  
  18.  
  19. callrouts()
  20. {
  21.     int16
  22.     i, dirobj;
  23.  
  24.     if( dwimi() < 0 )                    /* Iobj DWIMMING */
  25.     return;
  26.  
  27.     if( dwimd() < 0 )                /* Dobj DWIMMING */
  28.     return;
  29.  
  30.     for( (dirobj = NumDobj ? NumDobj : 1), i = 0; i < dirobj; i++ ) {
  31.     if( SET_EXIT( 2 ) != 0 )
  32.         /* $exit 2 */
  33.         continue;
  34.  
  35.     *Conj = Conj[ i ];
  36.     *Dobj = Dobj[ i ];
  37.  
  38.     Phase = 2;
  39.     if( SET_EXIT( 0 ) == 0 )
  40.         /* Actor ACTION */
  41.         callone( objspace[ CURRACT ].props[ _ACT - 17 ] );
  42.  
  43.     Phase = 3;
  44.     if( SET_EXIT( 0 ) == 0 )
  45.         /* Verb PREACT */
  46.         callone( verbspace[ Verb ].preact );
  47.  
  48.     Phase = 4;
  49.     if( SET_EXIT( 0 ) == 0 )
  50.         /* Indirect object ACTION */
  51.         callone( objspace[ ((Iobj < 0)?_STRING:Iobj) ].props[ _ACT - 17 ] );
  52.  
  53.     Phase = 5;
  54.     if( SET_EXIT( 0 ) == 0 )
  55.         /* Dobj ACTION */
  56.         callone( objspace[((*Dobj < 0)?_STRING:*Dobj)].props[ _ACT - 17 ] );
  57.  
  58.     Phase = 6;
  59.     if( SET_EXIT( 0 ) == 0 )
  60.         /* Verb ACTION */
  61.         callone( verbspace[ Verb ].postact );
  62.     }
  63.     CLR_EXIT( 2 );
  64.  
  65.     Phase = 7;
  66.     if( SET_EXIT( 0 ) == 0 )
  67.     /* ROOM ACTION */
  68.     callone( objspace[ objspace[ CURRACT ].loc ].props[ _ACT - 17 ] );
  69.     CLR_EXIT( 0 );
  70.  
  71.     CLR_EXIT( 1 );
  72. }
  73.  
  74.  
  75. execdems()
  76. {
  77.     int16
  78.     i;
  79.  
  80.     if( !numact ) {
  81.     fputs( "No actors active.  ADL aborting.\n", stderr );
  82.     head_term();
  83.     exit( -1 );
  84.     }
  85.     for( i = 0; i < numd; i++ )
  86.     callone( demons[ i ] );
  87. }
  88.  
  89.  
  90. execfuses()
  91. {
  92.     int16
  93.     i;
  94.  
  95.     for( i = numf - 1; i >= 0; i-- )
  96.     if( (ftimes[ i ] <= currturn) && (f_actors[ i ] == CURRACT) ) {
  97.         ip = routspace[ fuses[ i ] ];
  98.         push( 1 );        /* stackdepth */
  99.         push( bp );
  100.         push( 0 );            /* ip (make it 0 so runprog returns
  101.                        after executing just this fuse */
  102.         bp = sp - 3;        /* new bp */
  103.         runprog();
  104.         if( sp <= NUMVAR )        /* We did a $exit */
  105.         return;
  106.         pop();            /* retval */
  107.             delfuse( CURRACT, fuses[ i ] );
  108.     }
  109. }
  110.  
  111.  
  112. runprog()
  113. {
  114. #if DEBUG
  115.     address
  116.     tip;
  117. #endif
  118.     int16
  119.     instr,
  120.     t,
  121.     t1;
  122. #if DEBUG
  123.     int16
  124.     tbp,
  125.     tsp;
  126. #endif
  127.  
  128.     while( ip ) {
  129. #if DEBUG
  130.     tip = ip;
  131.     tbp = bp;
  132.     tsp = sp;
  133.     t1 = 0;
  134. #endif
  135.     instr = vm_get8( (int32)(ip++), &codetab );
  136.     if( instr & PUSHN ) {
  137.         if( instr & 0x07F )
  138.         t = 0xFF00 | instr;    /* Sign extend the thing */
  139.         else
  140.         t = 0;
  141.         instr = PUSHN;
  142.         push( t );
  143. #if DEBUG
  144.         t1 = t;
  145. #endif
  146.     }
  147.     else if( instr & (PUSHARG | PUSHLOCL | CALL) ) {
  148.         t = instr & 0x01F;
  149.         instr &= (PUSHARG | PUSHLOCL | CALL);
  150.         switch( instr ) {
  151.         case PUSHARG  :
  152.             if( t )
  153.             push( stack[ bp + t ] );
  154.             else
  155.             push( stack[ bp ] - 1 );
  156.             break;
  157.         case PUSHLOCL :
  158.             push( bp + stack[ bp ] + 2 + t );
  159.             break;
  160.         case CALL :
  161.             docall( t );
  162.             break;
  163.         }
  164. #if DEBUG
  165.         t1 = t;
  166. #endif
  167.     }
  168.     else if( instr & (PUSHS | JMP | JMPZ) ) {
  169.         switch( instr & (PUSHS | JMP | JMPZ) ) {
  170.         case PUSHS :
  171.             t = (instr & 0x03) << 8;
  172.             t |= (vm_get8( (int32)(ip++), &codetab ) & 0x0FF);
  173.             push( t );
  174. #if DEBUG
  175.             t1 = t;
  176. #endif
  177.             break;
  178.         case JMP :
  179.             t1 = vm_get16( (int32)ip, &codetab ) & 0x0FFFF;
  180.             ip = t1;
  181.             break;
  182.         case JMPZ  :
  183.             t1 = vm_get16( (int32)ip, &codetab ) & 0x0FFFF;
  184.             if( stack[ sp - 1 ] == 0 )
  185.             ip = t1;        /* Jump to the target */
  186.             else
  187.             ip += 2;        /* Skip the operand */
  188.             break;
  189.         }
  190.         instr &= (PUSHS | JMP | JMPZ);
  191.     }
  192.     else switch( instr ) {
  193.         case POP :
  194.         pop();
  195.         break;
  196.         case PUSHME :
  197.         push( CURRACT );
  198.         break;
  199.         case RET :
  200.         doret();
  201.         break;
  202.         case PUSH :
  203.         t = vm_get16( (int32)ip, &codetab );
  204.         ip += 2;        /* Skip over operand */
  205.         push( t );
  206. #if DEBUG
  207.         t1 = t;
  208. #endif
  209.         break;
  210. #if DEBUG
  211.         case FILEN :
  212.         foundfile = 1;
  213.         t = vm_get16( (int32)ip, &codetab );
  214.         ip += 2;
  215.         filenum = t;
  216.         t1 = t;
  217.         break;
  218.         case LINEN :
  219.         foundfile = 1;
  220.         t = vm_get16( (int32)ip, &codetab );
  221.         ip += 2;
  222.         linenum = t;
  223.         t1 = t;
  224.         break;
  225. #endif
  226.         default :
  227.         error( 11 );    /* Illegal instruction */
  228.     }
  229. #if DEBUG
  230.     if( debug ) {
  231.         fprintf( stderr, "ip = %d, bp = %d, sp = %d, instr = 0x%02x %d\n",
  232.                 tip, tbp, tsp, instr, t1 );
  233.         printstack();
  234.     }
  235. #endif
  236.     }
  237. }
  238.  
  239.  
  240. #if DEBUG
  241. push( x )
  242. int16
  243.     x;
  244. {
  245.     stack[ sp++ ] = x;
  246.     if( sp >= STACKSIZE )
  247.     error( 8 );        /* Stack overflow */
  248. }
  249.  
  250.  
  251. int16
  252. pop()
  253. {
  254.     if( sp <= NUMVAR )
  255.     error( 9 );        /* Stack underflow */
  256.     return stack[ --sp ];
  257. }
  258. #endif
  259.  
  260.  
  261. doret()
  262. {
  263.     int16
  264.     retval, tbp;
  265.  
  266.     retval = pop();
  267.     sp = bp + stack[ bp ] + 2;    /* Remove locals from stack */
  268.     popip();
  269.     tbp = pop();
  270.     sp = bp;
  271.     bp = tbp;
  272.     push( retval );
  273. }
  274.  
  275.  
  276. breaker()
  277. {
  278.     printf( "***BREAK***\n" );
  279.     exit( 1 );
  280. }
  281.  
  282.  
  283. docall( stackdepth )
  284. int16
  285.     stackdepth;
  286. {
  287.     int16
  288.     which;
  289.     
  290.     checkbreak( breaker );        /* Check for ^C */
  291.     push( bp );
  292.     puship();
  293.     bp = sp - stackdepth - 2;
  294.     which = stack[ bp ];
  295. #if DEBUG
  296.     if( debug )
  297.     fprintf( stderr, "Calling routine %d\n", which );
  298. #endif
  299.     stack[ bp ] = stackdepth;
  300.     if( which < 0 ) {
  301.     dosysfunc( which );
  302.     }
  303. #if DEBUG
  304.     else if( which > NUMROUT )
  305.     error( 10 );        /* Illegal routine call */
  306. #endif
  307.     else {
  308.     ip = routspace[ which ];
  309.     if( !ip )    /* null routine */ {
  310.         push( 0 );
  311.         doret();
  312.     }
  313.     }
  314. }
  315.  
  316.  
  317. #if DEBUG
  318. printstack()
  319. {
  320.     int16
  321.     i;
  322.  
  323.     fputs( "Stack = ", stderr );
  324.     for( i = NUMVAR; i < sp; i++ )
  325.     fprintf( stderr, "%04x ", stack[ i ] );
  326.     fputs( "\n", stderr );
  327. }
  328. #endif
  329.  
  330.  
  331. callone( rp )
  332. int16
  333.     rp;
  334. {
  335. #if DEBUG
  336.     if( debug )
  337.     fprintf( stderr, "Calling routine %d\n", rp );
  338. #endif
  339.  
  340.     if( !rp )
  341.     return;
  342. #if DEBUG
  343.     else if( (rp < 0) || (rp > NUMROUT) )
  344.     error( 12 );        /* Illegal routine call */
  345. #endif
  346.     bp = sp = NUMVAR;
  347.     ip = routspace[ rp ];
  348.     push( 1 );        /* stackdepth */
  349.     push( NUMVAR );    /* bp */
  350.     push( 0 );        /* ip */
  351.     runprog();
  352. }
  353.  
  354.  
  355. u_prompt()
  356. {
  357.     callone( prompter );
  358. }
  359.  
  360. /*** EOF adlmach.c ***/
  361.